<template>
    <div class="goodsinfo-container">
        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :lunbotuList="lunbotu" :full="false"></swiper>  
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">{{desc.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价：<del>¥{{desc.market_price}}</del>&nbsp;&nbsp;销售价：<span class="now_price">¥{{desc.sell_price}}</span>
                    </p>
                    <p>购买数量：<numbox @getNum="getNum" :stock_quantity="desc.stock_quantity"></numbox></p>
                    <p>
                        <mt-button size="small" type="primary">立即购买</mt-button>
                        <mt-button size="small" type="danger" @click="addToShoppCar">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品编号：{{desc.goods_no}}</p>
                    <p>库存情况：{{desc.stock_quantity}}</p>
                    <p>上架时间：{{desc.add_time | dateFormat}}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="getDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="getComment(id)">商品评论</mt-button>
            </div>
        </div>
    </div>
</template>

<script>
import swiper from '@/components/subcomponents/swiper'
import numbox from '@/components/subcomponents/goodsinfo_numbox'
export default {
    data(){
        return {
            id:this.$route.params.id,
            desc:{},
            lunbotu:{},
            ballFlag:false,
            buyCount:1
        }
    },
    created(){
        this.getLunbotu(),
        this.getInfo()
    },
    methods:{
        async getInfo(){
            const {data} = await this.$http.get('api/goods/getinfo/' + this.id)
            if(data.status === 0){
                this.desc = data.message[0]
            }
        },
        async getLunbotu(){
            const {data} = await this.$http.get('api/getthumimages/' + this.id)
            if(data.status === 0){
                data.message.forEach(ele => {
                    ele.img = 'https://farm6.staticflickr.com/5591/15008867125_68a8ed88cc_b.jpg'
                });
                this.lunbotu = data.message
            }
        },
        async getDesc(id){
            this.$router.push({name:'GoodsDesc',params:id})
        },
        async getComment(id){
            this.$router.push({name:'GoodsComment',params:id})
        },
        addToShoppCar(){
            this.ballFlag = !this.ballFlag
            var goodsinfo = {
                id:this.id,
                count:Number(this.buyCount),
                price:Number(this.desc.sell_price),
                selected:true
            }
            //调用store里的mutations
            this.$store.commit('addToCar',goodsinfo)
        },
        beforeEnter(el){
            el.style.transform = "translate(0,0)"
        },
        enter(el,done){
            el.offsetWidth;

            const ballPosition = this.$refs.ball.getBoundingClientRect()
            const badgePosition = document.getElementById('badge').getBoundingClientRect()
            const xDis = badgePosition.left - ballPosition.left
            const yDis = badgePosition.top - ballPosition.top

            el.style.transform = `translate(${xDis}px,${yDis}px)`;
            el.style.transition = "all 0.5s cubic-bezier(.4,-0.3,1,.68)";
            setTimeout(function(){
                done();
            },470)
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag;
        },
        getNum(count){
            this.buyCount = count
        }
    },
    components:{
        swiper,
        numbox
    }
}
</script>

<style lang="scss" scoped>
.goodsinfo-container{
   background-color: #eee;
   overflow: hidden;

    .now_price{
        color:red;
        font-weight:bold;
        font-size:16px;
    }
   .mui-card-footer{
        display: block;
        
        button{
            margin:15px 0;
        }
    }
     .ball{
        width: 15px;
        height: 15px;
        border-radius:50%;
        background-color: red; 
        position: absolute;
        z-index: 99;
        top: 390px;
        left: 149px;
    }
}

</style>
